#include "../include/Board.h"
#include "../include/Game.h"
#include "../include/KeyBoard.h"
#include "../include/NetServer.h"

Game::Game():m_board(nullptr), m_keyBoard(nullptr)
{
    m_board = new Board();
    m_keyBoard = new KeyBoard();
    netServer = new NetServer();
    mPoint.x = 0;
    mPoint.y = 0;
}
Game::~Game()
{
    delete m_board;
    m_board = nullptr;

    delete m_keyBoard;
    m_keyBoard = nullptr;

    delete netServer;
    netServer = nullptr;
}

void Game::init(int type)
{
    m_board->init();
    if(type == 0) return;
    if(!netServer->init())
    {
        perror("server connect fail");
        exit(-1);
    }
}

bool Game::setPoint(char ch)
{
    if(ch == m_keyBoard->UP)
    {
        if(mPoint.x == 0) 
        {
            mPoint.x = 0;
        }
        else
        {
            mPoint.x = mPoint.x - 1;
        }
    }
    else if(ch == m_keyBoard->DOWN)
    {
        if(mPoint.x == 2) 
        {
            mPoint.x = 2;
        }
        else
        {
            mPoint.x = mPoint.x + 1;
        }
    }
    else if(ch == m_keyBoard->LEFT)
    {
        if(mPoint.y == 0) 
        {
            mPoint.y = 0;
        }
        else
        {
            mPoint.y = mPoint.y - 1;
        }
    }
    else if(ch == m_keyBoard->RIGHT)
    {
        if(mPoint.y == 2) 
        {
            mPoint.y = 2;
        }
        else
        {
            mPoint.y = mPoint.y + 1;
        }
    }
    else if(ch == m_keyBoard->ENTER)
    {
        return false;
    }
    else if(ch == m_keyBoard->Q)
    {
        exit(0);
    }
    m_board->setPoint(mPoint);
    std::cout << "move mPoint.x:" << mPoint.x << " mPoint.y: " << mPoint.y << std::endl;
    return true;
    
}

void Game::play()
{
    int index = 0;

    XCY::NetPacket packInfo;
    while(true)
    {
        std::cout << "current player " << XCY::CHEER[index] << std::endl;

        //上下左右键设置位置，回车键下棋，Q键退出
        char ch = m_keyBoard->getCh();
        //设置下棋坐标，以及判断是否是下棋
        if(setPoint(ch))
        {
            continue;
        } 
        //用户选择的位置下棋，如果位置为空则下棋成功，否则继续
        packInfo.point.x = mPoint.x;
        packInfo.point.y = mPoint.y;
        packInfo.nCurrent_player = index;
        if(!m_board->updateBoard(packInfo))
        {
            continue;
        }
        //下完棋后判断是否胜利，胜利则结束游戏，否则判断棋盘棋子数量，删除最早落下的棋子
        if(m_board->isFinish(XCY::CHEER[index]))
        {
            std::cout << "player " << XCY::CHEER[index] << " win" << std::endl;
            break;
        }
        else
        {
            m_board->deleteone();
        }
        //换玩家
        index ^= 1;
    }
}

void Game::netPlay()
{
    //开始时需要等待读取服务端返回的内容
    int index = 0; //玩家执棋子样式

    XCY::PrepareStart pInfo;
    netServer->prepareToStart(pInfo); //由服务器返回初始化资源
    int round = 1;
    std::cout << "enter game, you are the player: " << XCY::CHEER[pInfo.nIndex] << std::endl;
    XCY::NetPacket packInfo;
    while(true)
    {
        //判断当前下棋是否是自己，不是自己需要等待
        if(pInfo.nIndex != pInfo.nCurIndex)
        {
            std::cout << "waiting..." << std::endl;
            netServer->parseBuff(packInfo); //获取对方下棋结果
            m_board->updateBoard(packInfo);
            if(m_board->isFinish(XCY::CHEER[packInfo.nCurrent_player]))
            {
                std::cout << "player " << XCY::CHEER[packInfo.nCurrent_player] << " win" << std::endl;
                break;
            }
            else
            {
                m_board->deleteone();
            }
            pInfo.nCurIndex = !packInfo.nCurrent_player;
        }

        std::cout << "you are player " << XCY::CHEER[pInfo.nIndex] << ", current player " << XCY::CHEER[pInfo.nIndex] << std::endl;
        //上下左右键设置位置，回车键下棋，Q键退出
        char ch = m_keyBoard->getCh();
        //设置下棋坐标，以及判断是否是下棋
        if(setPoint(ch))
        {
            continue;
        } 
        //用户选择的位置下棋，如果位置为空则下棋成功，否则继续
        XCY::NetPacket temp;
        temp.point.x = mPoint.x;
        temp.point.y = mPoint.y;
        temp.nCurrent_player = pInfo.nIndex;
        if(!m_board->updateBoard(temp))
        {
            continue;
        }
        packInfo.point.x = mPoint.x;
        packInfo.point.y = mPoint.y;
        pInfo.nCurIndex = packInfo.nCurrent_player;
        packInfo.nCurrent_player = pInfo.nIndex;
        //下完棋后判断是否胜利，胜利则结束游戏，否则判断棋盘棋子数量，删除最早落下的棋子
        if(m_board->isFinish(XCY::CHEER[pInfo.nIndex]))
        {
            std::cout << "player " << XCY::CHEER[pInfo.nIndex] << " win" << std::endl;
            packInfo.nWiner = pInfo.nIndex;
            netServer->packBuff(packInfo);
            break;
        }
        else
        {
            m_board->deleteone();
        }
        round++;
        packInfo.nRound = round;
        
        packInfo.nCurrent_player = pInfo.nIndex;
        netServer->packBuff(packInfo);        
    }
}